package com.aplink.generic.util.convertcount;

import java.util.ArrayList;

import com.aplink.generic.constant.type.SqlDataType;
import com.aplink.generic.pojo.ContentData;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.util.objectrule.ActionBaseRule;
import com.aplink.generic.util.objectrule.ActionCompareRule;
import com.aplink.generic.util.objectrule.TargetRule;
import com.aplink.generic.util.objectrule.TargetRuleCollection;

public class GenericFilter {

    public static boolean checkAttributedInsideContentData(
            final String attributeID, final String tableName,
            final ArrayList<ObjPropInfo> objPropInfoList) {

        boolean isInside = false;

        for (int i = 0; i < objPropInfoList.size(); i++) {
            final ObjPropInfo objPropInfo = objPropInfoList.get(i);

            if ((objPropInfo.getTable().compareToIgnoreCase(tableName) == 0)
                    && (objPropInfo.getColumnName().compareToIgnoreCase(
                            attributeID) == 0)) {
                isInside = true;
            }
        }
        return isInside;
    }

    public static boolean checkAttributeInsideObjPropInfoList(
            final String attributeId,
            final ArrayList<ObjPropInfo> arrMainObjPropInfo) {

        for (int i = 0; i < arrMainObjPropInfo.size(); i++) {
            final ObjPropInfo objPropInfo = arrMainObjPropInfo.get(i);
            if (objPropInfo.getExpression() == null) {
                if (attributeId
                        .compareToIgnoreCase(objPropInfo.getColumnName()) == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean checkUniqueTableNameObjPropInfoList(
            final ArrayList<ObjPropInfo> objPropInfoList, final String tableName) {
        int tempcount = 0;
        if (objPropInfoList.size() == 0) {
            return false;
        }

        for (int i = 0; i < objPropInfoList.size(); i++) {
            if (tableName
                    .compareToIgnoreCase(objPropInfoList.get(i).getTable()) == 0) {
                tempcount++;
            }
        }

        if (tempcount == objPropInfoList.size()) {
            return true;
        }

        return false;
    }

    public static SqlDataType getDataTypeOfAttributeFromObjectPropMappingName(
            final ArrayList<ObjPropInfo> objPropList, final String attributeID) {
        for (int i = 0; i < objPropList.size(); i++) {
            final ObjPropInfo objPropInfo = objPropList.get(i);
            final String name = objPropInfo.getColumnName();
            if (name.compareTo(attributeID) == 0) {
                return objPropInfo.getDataType();
            }
        }
        return null;
    }

    public static ObjPropInfo getKeyAttributeFromObjPropInfoList(
            final ArrayList<ObjPropInfo> objPropInfoList) {
        ObjPropInfo objPropInfo = new ObjPropInfo();

        for (int i = 0; i < objPropInfoList.size(); i++) {
            // find attributeKey
            objPropInfo = objPropInfoList.get(i);
            if (objPropInfo.isKey()) {
                return objPropInfo;
            }
        }

        return null;
    }

    public static ContentData getObjPropColOf(final String table,
            final ContentData listObjPropCol) {
        final ContentData objPropCol = new ContentData();
        objPropCol.setMainTable(table);
        for (int i = 0; i < listObjPropCol.size(); i++) {
            final ObjPropInfo objPropInfo = listObjPropCol.valueAt(i);
            boolean canInsert = false;
            ObjPropInfo propInfo = null;
            if (objPropInfo.getTable().equalsIgnoreCase(table)) {
                canInsert = true;
                propInfo = new ObjPropInfo(objPropInfo);
            } else if (objPropInfo.isFKey()
                    && objPropInfo.getForeignTable().equalsIgnoreCase(table)) {
                canInsert = true;
                propInfo = new ObjPropInfo(objPropInfo);
                propInfo.setRelationship(null);
                // propInfo.setBackUpTable(propInfo.getTable());
                // propInfo.setTableName(table);
            }
            if (objPropCol.contains(objPropInfo)) {
                canInsert = false;
            }
            if (canInsert) {

                objPropCol.put(objPropInfo.getFullColumnName(), propInfo);
                if (objPropInfo.isCreatedTime()) {
                    objPropCol.addCreatedTimeObject(propInfo);
                }
                if (objPropInfo.isSyncStatus()) {
                    objPropCol.addSyncStatus(propInfo);
                }
                if (objPropInfo.isImageServerPath()) {
                    objPropCol.setImageServerPath(propInfo);
                }
                if (objPropInfo.isImageLocalPath()) {
                    objPropCol.setImageLocalPath(propInfo);
                }
                if (objPropInfo.isKey()) {
                    objPropCol.addPrimaryKeys(propInfo);
                }
                if (objPropInfo.isFKey()
                        && (objPropInfo.getRelationship() != null)) {
                    objPropCol.addForiegnKeys(propInfo);
                }
            }

        }
        return objPropCol;
    }

    public static ObjPropInfo getObjPropInfoOfAccountId(
            final ArrayList<ObjPropInfo> mObjectPropInfo) {
        ObjPropInfo objPropInfo = new ObjPropInfo();

        for (int i = 0; i < mObjectPropInfo.size(); i++) {
            // find attributeKey
            objPropInfo = mObjectPropInfo.get(i);
            if (objPropInfo.isUsername()) {
                return objPropInfo;
            }
        }
        return null;
    }

    public static ObjPropInfo getObjPropInfoOfAccountPass(
            final ArrayList<ObjPropInfo> mObjectPropInfo) {
        ObjPropInfo objPropInfo = new ObjPropInfo();

        for (int i = 0; i < mObjectPropInfo.size(); i++) {
            // find attributeKey
            objPropInfo = mObjectPropInfo.get(i);
            if (objPropInfo.isPassword()) {
                return objPropInfo;
            }
        }

        return null;
    }

    public static ObjPropInfo getObjPropInfoOfSort(
            final ArrayList<ObjPropInfo> mObjectPropInfo) {
        ObjPropInfo objPropInfo = new ObjPropInfo();
        for (int i = 0; i < mObjectPropInfo.size(); i++) {
            objPropInfo = mObjectPropInfo.get(i);
            if (objPropInfo.isSort()) {
                return objPropInfo;
            }
        }
        return null;
    }

    public static ArrayList<ObjPropInfo> getObjPropListMainTable(
            final String tableName,
            final ArrayList<ObjPropInfo> totalObjPropList) {
        final ArrayList<ObjPropInfo> mainObjPropList = new ArrayList<ObjPropInfo>();

        for (int i = 0; i < totalObjPropList.size(); i++) {
            final ObjPropInfo objPropInfo = totalObjPropList.get(i);

            if (objPropInfo.getTable().compareToIgnoreCase(tableName) == 0) {
                mainObjPropList.add(objPropInfo);
            }
        }
        return mainObjPropList;
    }

    public static ArrayList<ArrayList<ObjPropInfo>> getObjPropListParentTable(
            final String tableName,
            final ArrayList<ObjPropInfo> totalObjPropList) {

        final ArrayList<ArrayList<ObjPropInfo>> parentObjPropList = new ArrayList<ArrayList<ObjPropInfo>>();

        for (int i = 0; i < totalObjPropList.size(); i++) {
            final ObjPropInfo objPropInfo = totalObjPropList.get(i);
            ArrayList<ObjPropInfo> currentParentObjPropList = new ArrayList<ObjPropInfo>();

            if (objPropInfo.getTable().compareToIgnoreCase(tableName) != 0) {
                currentParentObjPropList = GenericFilter
                        .getObjPropListMainTable(objPropInfo.getTable(),
                                totalObjPropList);

            }
            if ((currentParentObjPropList.size() != 0)
                    && !parentObjPropList.contains(currentParentObjPropList)) {
                parentObjPropList.add(currentParentObjPropList);
            }
        }
        return parentObjPropList;
    }

    public static String getTargetActivityFromRule(
            final TargetRuleCollection targetCollection,
            final GenericObject passedGenericObject) {

        if (targetCollection == null) {
            return null;
        }
        final ArrayList<TargetRule> arrTargetRules = targetCollection
                .getArrTargetRules();
        for (int i = 0; i < arrTargetRules.size(); i++) {
            final TargetRule currentRule = arrTargetRules.get(i);
            final ActionBaseRule absRule = currentRule.getActionBaseRule();
            if (absRule instanceof ActionCompareRule) {
                final boolean isSatisfied = absRule
                        .isSatisfiedActionRule(passedGenericObject);
                if (isSatisfied == true) {
                    return currentRule.getTargetActivity();
                }
            }

        }

        return null;
    }

    public static String initTableNameNameFromObjPropInfoListAtKeyAtt(
            final ArrayList<ObjPropInfo> objPropInfoList, final String tableName) {

        // we should make sure that whole the items in this ObjPropInfo list
        // must be the same Om_ID
        final boolean isUnique = GenericFilter
                .checkUniqueTableNameObjPropInfoList(objPropInfoList, tableName);
        if (isUnique == false) {
            return "";
        }

        ObjPropInfo objPropInfoKey = new ObjPropInfo();
        objPropInfoKey = GenericFilter
                .getKeyAttributeFromObjPropInfoList(objPropInfoList);
        if (objPropInfoKey == null) {
            return "";
        }

        return objPropInfoKey.getTable();

    }

}
